N = 25;
fn = strcat(num2str(N), '-trianlges.txt');
display(fn);
fid = fopen(fn, 'w');
idx = 1;
x_lb = -10;
y_lb = -10;
x_ub = 10;
y_ub = 10;
n = sqrt(N);
xstep = (x_ub - x_lb)/n;
ystep = (y_ub - y_lb)/n;
tri = [-0.5, 0, 0.5; 0, sqrt(3)/2, 0];

for xbnd = x_lb:xstep:(x_ub - xstep)
	for ybnd = y_lb:ystep:(y_ub - ystep)
        fprintf(fid, 'PIECE %d\r\nQUANTITY\r\n1\r\nNUMBER OF VERTICES\r\n3\r\nVERTICES (X,Y)\r\n', idx);
        xt = rand(1, 1)*xstep + xbnd;
        yt = rand(1, 1)*ystep + ybnd;
        ttri = tri + [xt, xt, xt; yt, yt, yt];
        c = ttri(:,1) + ttri(:,2) + ttri(:,3);
        c = c./3;
        angle = rand(1, 1)*3.14 + 3.14;
        mat = [cos(angle), -sin(angle); sin(angle), cos(angle)];
        for i = 1:3
            ttri(:,i) = mat*(ttri(:,i)-c) + c; 
            s = sprintf('%.6f %.6f\r\n', ttri(1,i), ttri(2, i));
            %disp(s);
            fprintf(fid, s);
        end
        fprintf(fid, '\r\n');
        idx = idx + 1;
	end
end

%pedro sander
%downhill simplex